<?php 
class MyRoute extends CakeRoute {

/**
 * Override the parsing function to find an id based on a slug
 *
 * @param string $url Url string
 * @return boolean
 */
    public function parse($url) {
    	//home route    
    		
    	if($url=='/' || $url=='')
    		return array('controller'=>'homes','action'=>'index','pass'=>array());
 
    	//check seo enable
    	if(!Configure::read('SEO'))
    		return false;
    	//admin rou
    	if($url=='/'.Configure::read('FakeAdmin') || substr($url, 0,strlen(Configure::read('FakeAdmin'))+2)=='/'.Configure::read('FakeAdmin').'/' || substr($url, 0,7)=='/'.Configure::read('FakeAdmin').'#'  )return false;
		//parse url
		
		$url=split('\/',$url);		
		$searchurl='/'.$url[1];
		unset($url[0]);
		unset($url[1]);
		
		//try update cache
    	App::uses('CommonComponent', 'Controller/Component');
    	$Common=& new CommonComponent(new ComponentCollection());    	
    	$friendlyUrls=$Common->getCacheRoute();
    	if(empty($friendlyUrls)){
    		$Common->updateCacheRoute();	
    		$friendlyUrls=$Common->getCacheRoute();
    	}

    	if(!empty($friendlyUrls)){
	    	if(array_key_exists($searchurl, $friendlyUrls))
	    	{
	    		
	    		//parse realurl
	    		$realUrl=$friendlyUrls[$searchurl];
				
	    		
	    		$realUrl=split('\/',$realUrl);
				if(!isset($realUrl[1]) || empty($realUrl[1]))$realUrl[1]='homes';
				if(!isset($realUrl[2]) || empty($realUrl[2]))$realUrl[2]='index';
	    		$params=array('controller'=>$realUrl[1],'action'=>$realUrl[2],'named'=>array());
	    		unset($realUrl[0]);
	    		unset($realUrl[1]);
	    		unset($realUrl[2]);
	    		//merge pass param from url and real url
	    		$params['pass']=array_merge($realUrl,$url);
				return $params;
	    	}
	    	else if($redirectUrl=array_search($searchurl, $friendlyUrls)){
	    		header( 'Location: '.SITE_URL.$redirectUrl ) ;
	    	}
    	}
    	header( 'Location: '.SITE_URL ) ;    	
    }
}
?>
